﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using PlugNT.Custom;
using PlugNT.Cms.Core;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Model;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_Article
{
    public partial class InterfaceAdd : System.Web.UI.Page     //AdminPage
    {
        private Article curArticle = new Article();
        private Channel curChannel = new Channel();
        private Tables curTables = new Tables();
        private string currpostfix = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                currpostfix = (Request.QueryString["currpostfix"] == null) ? WebConfig.TablePostfix : Request.QueryString["currpostfix"].Trim();
                TablesInfo tinfo = WebConfig.IsAccess ? curTables.GetAccessTablesInfo() : curTables.GetTablesInfoByPostfix(currpostfix);

                if (tinfo != null)
                {
                    if (h_adminpassword.Value.Trim() == WebConfig.AdminInterfaceKey)
                    {
                        ArticleInfo model = new ArticleInfo();
                        string clsno = h_clsno.Value.Trim();
                        string createpath = h_createpath.Value.Trim();
                        string t_createpath = "";
                        string t_showfile = "";
                        if (clsno != "")
                        {
                            Templet tmpTemplet = new Templet();
                            DataTable dtTemplet = tmpTemplet.GetListByClsno(clsno);
                            if (dtTemplet.Rows.Count > 0)
                            {
                                DataRow drTemplet = dtTemplet.Rows[0];
                                t_createpath = (tinfo.dicpath == "") ? "/" : "/" + tinfo.dicpath + "/";
                                t_createpath = t_createpath + drTemplet["createpath"].ToString();
                                t_showfile = drTemplet["showfile"].ToString();
                            }
                        }

                        model.no = PlugNT.Common.Utils.GetDateTimeCode() + Session.SessionID.GetHashCode().ToString();
                        model.images = h_images.Value.Trim();
                        model.title = h_title.Value.Trim();
                        model.subtitle = h_subtitle.Value.Trim();
                        model.keyword = h_keyword.Value.Trim();
                        model.description = h_description.Value.Trim();
                        model.clsno = h_clsno.Value.Trim();
                        model.clslist = curChannel.GetClslistByClsno(clsno, false);
                        model.contents = h_contents.Value.Trim();
                        model.jumpurl = h_jumpurl.Value.Trim();
                        model.info = h_info.Value.Trim();
                        model.author = h_author.Value.Trim();
                        model.url = h_url.Value.Trim();
                        model.tip = int.Parse(h_tip.Value.Trim());
                        model.tag = h_tag.Value.Trim();
                        model.hits = int.Parse(h_hits.Value.Trim());
                        model.orders = int.Parse(h_orders.Value.Trim());
                        model.commend = (h_commend.Value.Trim() == "true");
                        model.stick = (h_stick.Value.Trim() == "true");
                        model.publish = (h_publish.Value.Trim() == "true");
                        model.addtime = DateTime.Now;
                        model.createpath = (createpath == "") ? t_createpath : "/" + createpath.Trim('/') + "/";
                        model.pagename = h_pagename.Value.Trim();

                        //设置扩展字段
                        string s_extfield = h_extfield.Value.Trim();
                        if (s_extfield != "")
                        {
                            string[] extArr = s_extfield.Split('&');
                            foreach (string nvs in extArr)
                            {
                                string[] nv = nvs.Split('=');
                                if (nv.Length > 1)
                                {
                                    string[] sArr = nv[0].Split('_');
                                    if (sArr.Length == 2)
                                    {
                                        FieldModel finfo = new FieldModel();
                                        finfo.fieldName = sArr[0];
                                        finfo.fieldType = sArr[1];
                                        finfo.fieldValue = nv[1];
                                        model.AppExtField(finfo);
                                    }
                                }
                            }
                        }


                        if (model.title != "")
                        {
                            if (model.clsno != "")
                            {
                                curArticle.CurPostfix = currpostfix;
                                if (curArticle.Add(model))
                                {
                                    string id = curArticle.GetId(model.no);
                                    model.id = int.Parse(id);
                                    //更新页面名
                                    if (model.pagename == "")
                                    {
                                        string tmpFileName = PlugNT.Common.Utils.GetFileName(t_showfile);
                                        int tmpFlag = tmpFileName.LastIndexOf(".");
                                        tmpFileName = (tmpFlag > 0) ? tmpFileName.Substring(0, tmpFlag) : tmpFileName;

                                        string cmsPreShow = tmpFileName;
                                        model.pagename = cmsPreShow + id.ToString();
                                    }
                                    DataRow dr = curArticle.GetPreviousDataRow(model.id, model.clsno);
                                    if (dr != null)
                                    {
                                        string hurl = (dr["jumpurl"].ToString() != "") ? dr["jumpurl"].ToString() : dr["createpath"].ToString() + dr["pagename"].ToString() + ".shtml";
                                        model.previouspage = "<a id=\"pntPrevious" + model.id.ToString() + "\" href=\"" + hurl + "\">" + dr["title"].ToString() + "</a>";
                                    }
                                    dr = curArticle.GetNextDataRow(model.id, model.clsno);
                                    if (dr != null)
                                    {
                                        string hurl = (dr["jumpurl"].ToString() != "") ? dr["jumpurl"].ToString() : dr["createpath"].ToString() + dr["pagename"].ToString() + ".shtml";
                                        model.nextpage = "<a id=\"pntNext" + model.id.ToString() + "\" href=\"" + hurl + "\">" + dr["title"].ToString() + "</a>";

                                        string tmpHurl = (model.jumpurl != "") ? model.jumpurl : model.createpath + model.pagename + ".shtml";
                                        string tmpPreviouspage = "<a id=\"pntPrevious" + dr["id"].ToString() + "\" href=\"" + tmpHurl + "\">" + model.title + "</a>";
                                        curArticle.UpdatePreviousPage(int.Parse(dr["id"].ToString()), tmpPreviouspage);
                                    }
                                    curArticle.UpdatePart(model);

                                    HtmlBuilder html = new HtmlBuilder();
                                    html.TablePostFix = currpostfix;

                                    if (h_createhtml.Value.Trim() != "true")
                                    {
                                        Response.Write("add-ok-send-ok");
                                    }
                                    else
                                    {
                                        string error = PageBuilder.CreateShowHtml(html, id, model.pagename, model.createpath, t_showfile, clsno);
                                        if (error == "")
                                        {
                                            if (Sys.Info.articleupdated == false)
                                            {
                                                Sys.Info.articleupdated = true;
                                                Sys.UpdateArticleupdated();
                                            }
                                            Response.Write("add-ok-send-ok");
                                        }
                                        else
                                        {
                                            Response.Write("add-ok-生成页面错误！");
                                        }
                                    }
                                }
                                else
                                {
                                    Response.Write("add-error");
                                }
                            }
                            else
                            {
                                Response.Write("add-error-clsno 为空！");
                            }
                        }
                        else
                        {
                            Response.Write("add-error-title 为空！");
                        }
                    }
                    else
                    {
                        Response.Write("add-error-adminpassword 错误！");
                    }
                }
                else
                {
                    Response.Write("add-error-tablesisnull 错误！");
                }

            }
            catch (Exception ex)
            {
                Response.Write("add-error-errorstring:" +ex.Message + ex.StackTrace);
            }
        }

    }
}


